package com.larry.mr.outputformat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * @ProjectName hadoop
 * @Author larry
 * @Date 15:10 2021/11/16
 * @Description TODO
 **/
public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    private String path1 = "C:\\Users\\larry\\IdeaProjects\\hadoop\\MapReduce\\src\\main\\resources\\log\\out\\1.txt";
    private String path2 = "C:\\Users\\larry\\IdeaProjects\\hadoop\\MapReduce\\src\\main\\resources\\log\\out\\2.txt";
    private FileSystem fs;
    private final FSDataOutputStream out1;
    private final FSDataOutputStream out2;

    public LogRecordWriter(TaskAttemptContext job) throws IOException {
        fs = FileSystem.get(job.getConfiguration());
        //输出流
        out1 = fs.create(new Path(path1));
        out2 = fs.create(new Path(path2));
    }

    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        String s = key.toString();
        if (s.contains("atguigu")){
            out1.writeBytes(s + "\n");
        }else {
            out2.writeBytes(s + "\n");
        }
    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        IOUtils.closeStream(out1);
        IOUtils.closeStream(out2);
    }
}
